﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DrugsShopManagement.DBCLASS;
using System.Data.SqlClient;

namespace DrugsShopManagement
{
    public partial class FrmBackup : DevExpress.XtraEditors.XtraForm
    {
        public FrmBackup()
        {
            InitializeComponent();
        }
        Operator op = new Operator();
        private void button6_Click(object sender, EventArgs e)
        {
            openFileDialog1.FilterIndex = 0;
            openFileDialog1.FileName = "";
            //过滤文件的类型，
            openFileDialog1.Filter = "txt files (*.bak)|*.bak|All files (*.*)|*.*";


            if (openFileDialog1.ShowDialog() == DialogResult.OK)//打开文件浏览对话框并且点击确定后
            {
                txtPath1.Text = openFileDialog1.FileName.ToString();//取得选中的文件名
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtPath.Text = folderBrowserDialog1.SelectedPath.ToString();
            }
        }

        private void button2_Click(object sender, EventArgs e)//数据还原
        {
            try
            {
                if (txtPath.Text != "" && txtName.Text != "")
                {
                    string G_Str_ConnectionString = "server=.;database=Drugs;uid=sa;pwd=123456;Asynchronous Processing=True;";
                    SqlConnection conn = new SqlConnection(G_Str_ConnectionString);
                    conn.Open();
                    
                    //构造备份数据库的语句
                    string strBacl = "backup database Drugs to disk='" + txtPath.Text.Trim() + "\\" + txtName.Text.Trim() + ".bak'";
                    SqlCommand Cmd = new SqlCommand(strBacl, conn);
                    if (Cmd.ExecuteNonQuery() != 0)
                    {
                        MessageBox.Show("数据备份成功！", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("数据备份失败！", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
                else
                {
                    MessageBox.Show("请填写备份的正确位置及文件名！", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }// end 
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message.ToString());
            }
        }

        private void button5_Click(object sender, EventArgs e)//数据备份
        {
             if (txtPath1.Text != "")
            {

                string G_Str_ConnectionString = "server=.;database=Drugs;uid=sa;pwd=123456;Asynchronous Processing=True;";
                SqlConnection conn1 = new SqlConnection(G_Str_ConnectionString);
                conn1.Open();
                if (conn1.State == ConnectionState.Open)
                {
                    conn1.Close();
                }

                //-------------------杀掉所有连接 Drugs 数据库的进程--------------

                string G_Str_ConnectionString1 = "server=.;database=Drugs;uid=sa;pwd=123456;Asynchronous Processing=True;";
                SqlConnection conn2 = new SqlConnection(G_Str_ConnectionString1);
                conn2.Open();

                string strSQL = "select spid from master..sysprocesses where dbid=db_id( 'Drugs') ";
                SqlDataAdapter Da = new SqlDataAdapter(strSQL, conn2);//将连接和命令关联
                DataTable spidTable = new DataTable();//创建一个数据表对象
                Da.Fill(spidTable);  //将查询到数据放进表里面 内存中的虚拟表          
                SqlCommand Cmd = new SqlCommand();
                Cmd.CommandType = CommandType.Text;
                Cmd.Connection = conn2;

                for (int iRow = 1; iRow <= spidTable.Rows.Count - 1; iRow++)
                {
                    Cmd.CommandText = "kill " + spidTable.Rows[iRow][0].ToString();   //强行关闭用户进程  关闭已经打开的数据库连接
                    Cmd.ExecuteNonQuery();
                }
                conn2.Close();
                conn2.Dispose();

                //--------------------------------------------------------------------
                

                string G_Str_ConnectionString3 = "server=.;database=Drugs;uid=sa;pwd=123456;Asynchronous Processing=True;";
                SqlConnection conn3 = new SqlConnection(G_Str_ConnectionString3);
                conn3.Open();
                //备份数据库 然后又还原数据库
                SqlCommand sqlCmd = new SqlCommand("backup log Drugs to disk='"
                    + txtPath1.Text.Trim() + "'use master restore database Drugs from disk='"
                    + txtPath1.Text.Trim() + "'with replace", conn3);
                sqlCmd.ExecuteNonQuery();
                sqlCmd.Dispose();
                conn3.Close();
                conn3.Dispose();
                MessageBox.Show("数据还原成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                MessageBox.Show("为了必免数据丢失，在数据库还原后将关闭整个系统。");
                Application.Exit();
            }
             else
             {
                 MessageBox.Show("请选择备份文件！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
             }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}